'''urllib_handler处理器使用
为什么要学习handler?
    urllib.request.urlopen(ur1)
        不能定制请求头
    urllib.request.Request(url,headers,data)
        可以定制请求头
    Handler
        定制更高级的请求头(随着业务逻辑的复杂 请求对象的定制已经满足不了我们的需求(动态cookie和代理不能使用请求对象的定制)
'''

import urllib.request

url = 'https://www.baidu.com'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}

# 定制request
request = urllib.request.Request(url=url, headers=headers)

# handler   build_opener    open
# 1.获取handler
handler = urllib.request.HTTPHandler()
# 2.获取opener
opener = urllib.request.build_opener(handler)
# 3.使用open
response = opener.open(request)

content = response.read().decode('utf-8')
print(content)




